package com.zut.hospital.service.pharmacy;

import com.alibaba.fastjson.JSON;
import com.zut.hospital.dao.pharmacy.SupplierMapper;
import com.zut.hospital.entity.pharmacy.Supplier;
import com.zut.hospital.entity.pharmacy.SupplierExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: hos-test
 * @description:
 * @author: 杨曼珍
 * @create: 2019-06-07 16:08
 **/
@Service
public class SupServiceImpl implements SupService {
    @Autowired
    SupplierMapper supplierMapper;
    @Override
    public String layuiJsonParse(List<Map<String, String>> list, int size) {
        Map<String, Object> layuiMap = new HashMap<>();
        layuiMap.put("code", 0);
        layuiMap.put("msg", "success");
        layuiMap.put("count", size);
        layuiMap.put("data", list);
        return JSON.toJSONString(layuiMap);
    }

    public Map selectAll(Supplier supplier){
        Map<String,String>map;

        map=new HashMap<>();
        map.put("id",supplier.getId().toString());
        map.put("sup_name",supplier.getSupName());
        map.put("sup_contact_info",supplier.getSupContactInfo());
        map.put("remark",supplier.getSupRemark());
        return map;
    }
    @Override
    public String getAllSup(){
        SupplierExample supplierExample=new SupplierExample();
        List<Supplier>supplierList=supplierMapper.selectByExample(supplierExample);
        List<Map<String, String>> resultList = new ArrayList<>();
        for (Supplier supplier:supplierList){
            resultList.add(selectAll(supplier));
        }
        return layuiJsonParse(resultList,resultList.size());
    }
    @Override
    public String updateSup(int id,String field,String value){
        SupplierExample supplierExample=new SupplierExample();
        supplierExample.createCriteria().andIdEqualTo(id);
        Supplier supplier=supplierMapper.selectByExample(supplierExample).get(0);
        List<Map<String, String>> resultList = new ArrayList<>();
        if (field.equals("sup_name")){
            supplier.setSupName(value);
        }
        else {
            supplier.setSupContactInfo(value);
        }
        supplierMapper.updateByExample(supplier,supplierExample);
        return layuiJsonParse(resultList,resultList.size());
    }
    @Override
    public String addSup(String name,String contract,String remark){
        Supplier supplier=new Supplier();
        supplier.setSupName(name);
        supplier.setSupContactInfo(contract);
        supplier.setSupRemark(remark);

        supplierMapper.insert(supplier);

        Map<String, Object> layuiMap = new HashMap<>();
        layuiMap.put("code", 0);
        layuiMap.put("msg", "success");
        layuiMap.put("count", 0);
        layuiMap.put("data", "");
        return JSON.toJSONString(layuiMap);
    }
}
